package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.Curtain;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 窗帘数据库操作
 */

public class CurtainDao {
    private Context mContext;
    private Dao<Curtain, Integer> curtainDaoOpe;
    private DatabaseHelper helper;

    public CurtainDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            curtainDaoOpe = helper.getDao(Curtain.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(Curtain curtain) {
        try {
            curtain.setChecked(false);
            curtainDaoOpe.create(curtain);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateCurtain(Curtain curtain) {
        try {
            curtain.setChecked(false);
            return curtainDaoOpe.update(curtain);
            //    LogHelper.i("updateLightCurtainnumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public Curtain get(int id) {
        try {
            return curtainDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Curtain getByUuid(String uuid) {
        List<Curtain> curtainList = null;
        try {
            curtainList = curtainDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (curtainList != null && curtainList.size() > 0) {
                return curtainList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<Curtain> getAllCurtain() {
        List<Curtain> curtainList = new ArrayList<>();
        try {
            curtainList = curtainDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return curtainList;
    }

    public Curtain getById(String groupId,int category) {
        List<Curtain> curtainList = null;
        try {
            curtainList = curtainDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (curtainList != null && curtainList.size() > 0) {
                return curtainList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<Curtain> getFamilyCurtains(String familyId,int category) {
        List<Curtain> curtainList = new ArrayList<>();
        try {
            curtainList = curtainDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return curtainList;
    }

    public int delete(Curtain curtain) {
        try {
            return curtainDaoOpe.delete(curtain);
            //    LogHelper.i("deleLightCurtainnumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = curtainDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<Curtain> curtainList) {
        try {
            int number = curtainDaoOpe.delete(curtainList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyCurtains(String familyId) {
        try {
            List<Curtain> curtainList = getFamilyCurtains(familyId,6);
            int number = curtainDaoOpe.delete(curtainList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Curtain getCurtainByName(String familyId, String name,int category) {
        try {
            List<Curtain> curtainList = curtainDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (curtainList != null && curtainList.size() > 0)
                return curtainList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
